/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javagameengine;

/**
 *
 * @author jimi
 */
public class Vector
{

	public double x, y, z;
	public static final Vector i = new Vector( 1, 0, 0 );
	public static final Vector j = new Vector( 0, 1, 0 );
	public static final Vector k = new Vector( 0, 0, 1 );

	public Vector()
	{
		x = y = z = 0;
	}

	/*
	 * @param a assigned x value
	 * @param b assigned y value
	 * @param c assigned z value
	 */
	public Vector( double a, double b, double c )
	{
		x = a;
		y = b;
		z = c;
	}

	/*
	 * @param a assigned x value
	 * @param b assigned y value
	 */
	public Vector( double a, double b )
	{
		x = a;
		y = b;
		z = 0;
	}

	public Vector( Vector other )
	{
		x = other.x;
		y = other.y;
		z = other.z;
	}

	public Point plus( Point p )
	{
		return new Point( p.x + x, p.y + y, p.z + z );
	}

	public Vector plus( Vector other )
	{
		return new Vector( other.x + x, other.y + y, other.z + z );
	}

	public Vector minus( Vector other )
	{
		return new Vector( x - other.x, y - other.y, z - other.z );
	}

	public double dot( Vector other )
	{
		return x * other.x + y * other.y + z * other.z;
	}

	public static Vector translate( Point p )
	{
		Vector v = new Vector();

		v.x = p.x;
		v.y = p.y;
		v.z = p.z;

		return v;
	}

	public double magnitude_squared()
	{
		return dot( this );
	}

	public double magnitude()
	{
		return Math.sqrt( magnitude_squared() );
	}

	public double length()
	{
		return magnitude();
	}

	public Vector times( double scalar )
	{
		return new Vector( x * scalar, y * scalar, z * scalar );
	}

	public Vector project( Vector u )
	{
		double scalar = dot( u ) / magnitude_squared();
		return times( scalar );
	}

	public Vector normal()
	{
		return times( 1.0 / length() );
	}

	public void normalize()
	{
		Vector v = normal();
		x = v.x;
		y = v.y;
		z = v.z;
	}

	public Vector negative()
	{
		Vector ret = new Vector();

		ret.x = -x;
		ret.y = -y;
		ret.z = -z;

		return ret;
	}

	public Vector cross( Vector other )
	{
		Vector ret = new Vector();

		ret.x = y * other.z - other.y * z;
		ret.y = other.x * z - x * other.z;
		ret.z = x * other.y - other.x * y;

		return ret;
	}
}
